package com.itheima.server;

import java.io.*;
import java.net.Socket;
import java.util.Properties;

public class MyRunable implements Runnable {
    private Properties prop;
    private Socket socket;


    public MyRunable(Properties prop, Socket socket) {
        this.prop = prop;
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while (true) {
                String choose = br.readLine();
                switch (choose) {
                    case "login" -> login(br);
                    case "rigister" -> rigister(br);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //获取用户登录时，传递过来的信息。
    //并进行判断
    public void login(BufferedReader br) throws IOException {
        System.out.println("用户选择了登录操作");
        String line = br.readLine();
        //username=zhangsan&password=123
        String[] Infoarr = line.split("&");
        String usernameStr = Infoarr[0].split("=")[1];
        String passwordStr = Infoarr[1].split("=")[1];

        if (prop.containsKey(usernameStr)) {
            //表示用户名存在，判断密码是否正确
            String rightPassword = (String) prop.get(usernameStr);
            if (rightPassword.equals(passwordStr)) {
                //表示密码也正确
                writeMessage2Client("1");
                //登录成功的时候，就需要把客户端的连接对象Socket保存起来
                Server.list.add(socket);
                //接收客户端发送过来的消息，并打印在控制台
                talk2All(br, usernameStr);
            } else {
                //表示密码不正确
                writeMessage2Client("2");
            }
        } else {
            //表示用户名不存在，给客户端提示
            writeMessage2Client("3");
        }
    }

    public void rigister(BufferedReader br) throws IOException {
        System.out.println("用户选择了注册操作");
        String line = br.readLine();
        String[] Infoarr = line.split("&");
        String usernameStr = Infoarr[0].split("=")[1];
        Server.list.add(socket);
        talk2All(br, usernameStr);
    }

    public void talk2All(BufferedReader br, String username) throws IOException {
        while (true) {
            String message = br.readLine();
            System.out.println(username + "发送了一条消息：" + message);

            //群发操作
            for (Socket socket : Server.list) {
                //s依次表示每一个客户端的连接对象
                writeMessage2Client(socket, username + "发送了一条消息：" + message);
            }
        }
    }

    public void writeMessage2Client(String message) throws IOException {
        //获取输出流
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write(message);
        bw.newLine();
        bw.flush();
    }

    public void writeMessage2Client(Socket s, String message) throws IOException {
        //获取输出流
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        bw.write(message);
        bw.newLine();
        bw.flush();
    }
}
